#!/bin/bash
####################
# This utility searches for available HFS+, NTFS and FAT32 partitions, creates
# mount points for them and mounts the partitions
#
# Modified for Amahi by Carlos Puchol (cpg at amahi)
# Amahi cannot assume liability for any issues arising from using
# this script! You decline any warranty by using this script!
#
# (c)2008 Luigi Capriotti <l.capriotti@tiscali.it> for use in LiveXBMCV2
# Base on "diskmount" by Dennis Kaarsemaker <dennis@ubuntu-nl.org>
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.

# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
###################

VERBOSE='y'
BASEDIR='/var/hda/files/drives'

SELECTED_DRIVE=''
if [ $# -gt 0 ]; then
	if [[ $1 == "--silent" ]]; then
		VERBOSE='n'
		if [ $# -gt 1 ]; then
			SELECTED_DRIVE=$2
		fi
	else
		SELECTED_DRIVE=$1
	fi 
fi

# Root check
if [ $UID != 0 ]; then
    echo 'You should run this program as root or using sudo'
    exit 1
fi

NTFSOPTIONS='user,fmask=0113,dmask=0002,uid=500,gid=100,noatime'
FATOPTIONS='user,fmask=0113,dmask=0002,uid=500,gid=100,noatime'
HFSOPTIONS='user,uid=500,gid=100'
EXTOPTIONS=''

# Now for the real work
drivesntfs=`fdisk -l 2> /dev/null | grep -i 'ntfs' | awk -F '/| ' '{print $3}'`
drivesfat=`fdisk -l  2> /dev/null | grep -i 'fat32' | awk -F '/| ' '{print $3}'`
driveshfs=`fdisk -l  2> /dev/null | grep -i 'HFS' | awk -F '/| ' '{print $3}'`
drivesext=`fdisk -l  2> /dev/null | grep -i 'Linux' | egrep -v 'swap|LVM' | awk -F '/| ' '{print $3}'`

if [ -f $BASEDIR ]; then
	echo "Error: $BASEDIR is a file; needs to be a directory, or change the BASEDIR variable in `which hda-diskmount`"
	exit 1
fi

if [ ! -d $BASEDIR ]; then
    mkdir -p $BASEDIR
fi

NEXT_DRIVE='1'
DRIVE_MOUNTED='n'

for drive in $drivesntfs; do
	if [[ $SELECTED_DRIVE != "" && $SELECTED_DRIVE != "/dev/$drive" ]]; then
		continue
	fi
    [ $VERBOSE == 'y' ] && echo "****************************************************************"
    uuid=`ls -l /dev/disk/by-uuid/ | grep "\.\./\.\./$drive" | awk '{print $(NF-2)}'`
    if [[ `grep "/dev/$drive" /etc/fstab` || `grep "UUID=$uuid" /etc/fstab` || `pmount | grep $drive` || `mount | grep $drive` ]]; then
        [[ $VERBOSE == 'y' && `pmount | grep $drive` ]] && echo "Ignoring /dev/$drive - already mounted"
        [[ $VERBOSE == 'y' && `mount | grep $drive` ]] && echo "Ignoring /dev/$drive - already mounted"
        [[ $VERBOSE == 'y' && `grep "/dev/$drive" /etc/fstab` ]] && echo "Ignoring /dev/$drive - already in /etc/fstab as /dev/$drive"
        [[ $VERBOSE == 'y' && `grep "UUID=$uuid" /etc/fstab` ]] && echo "Ignoring /dev/$drive - already in /etc/fstab as UUID=$uuid"
    else
        if [ -e /sys/block/${drive%?}/removable ]; then
            if [ "$(cat /sys/block/${drive%?}/removable)" == "1" ]; then
		        [ $VERBOSE == 'y' ] && echo "Ignoring /dev/$drive - removable drive"
            else
				while [ -e $BASEDIR/drive$NEXT_DRIVE ]; do
					NEXT_DRIVE=`echo $NEXT_DRIVE + 1 | bc`
				done
                mkdir $BASEDIR/drive$NEXT_DRIVE
                mount -t ntfs-3g -o rw,$NTFSOPTIONS /dev/$drive $BASEDIR/drive$NEXT_DRIVE

                DRIVE_MOUNTED='y'
                if [ $VERBOSE == 'n' ]; then
					echo "$BASEDIR/drive$NEXT_DRIVE"
				else
                    echo "Mounted /dev/$drive as '$BASEDIR/drive$NEXT_DRIVE'"
                    echo -e "\tYou may want your system to mount it every time you boot."
                    echo -e "\tTo do so, add this line VERY CAREFULLY to /etc/fstab and reboot:"
                    if [ "$uuid" == "" ]; then
                        echo -e "\t/dev/$drive $BASEDIR/drive$NEXT_DRIVE ntfs-3g rw,$NTFSOPTIONS 1 2"
                    else
                        echo -e "\tUUID=$uuid $BASEDIR/drive$NEXT_DRIVE ntfs-3g rw,$NTFSOPTIONS 1 2"
                    fi
                fi
            fi
        fi
    fi
done

for drive in $drivesfat; do
	if [[ $SELECTED_DRIVE != "" && $SELECTED_DRIVE != "/dev/$drive" ]]; then
		continue
	fi
    [ $VERBOSE == 'y' ] && echo "****************************************************************"
    uuid=`ls -l /dev/disk/by-uuid/ | grep "\.\./\.\./$drive" | awk '{print $(NF-2)}'`
    if [[ `grep "/dev/$drive" /etc/fstab` || `grep "UUID=$uuid" /etc/fstab` || `pmount | grep $drive` || `mount | grep $drive` ]]; then
        [[ $VERBOSE == 'y' && `pmount | grep $drive` ]] && echo "Ignoring /dev/$drive - already mounted"
        [[ $VERBOSE == 'y' && `mount | grep $drive` ]] && echo "Ignoring /dev/$drive - already mounted"
        [[ $VERBOSE == 'y' && `grep "/dev/$drive" /etc/fstab` ]] && echo "Ignoring /dev/$drive - already in /etc/fstab as /dev/$drive"
        [[ $VERBOSE == 'y' && `grep "UUID=$uuid" /etc/fstab` ]] && echo "Ignoring /dev/$drive - already in /etc/fstab as UUID=$uuid"
    else
        if [ -e /sys/block/${drive%?}/removable ]; then
            if [ "$(cat /sys/block/${drive%?}/removable)" == "1" ]; then
		        [ $VERBOSE == 'y' ] && echo "Ignoring /dev/$drive - removable drive"
            else
				while [ -e $BASEDIR/drive$NEXT_DRIVE ]; do
					NEXT_DRIVE=`echo $NEXT_DRIVE + 1 | bc`
				done
                mkdir $BASEDIR/drive$NEXT_DRIVE
                mount -t vfat -o rw,$FATOPTIONS /dev/$drive $BASEDIR/drive$NEXT_DRIVE

                DRIVE_MOUNTED='y'
                if [ $VERBOSE == 'n' ]; then
					echo "$BASEDIR/drive$NEXT_DRIVE"
				else
                    echo "Mounted /dev/$drive as '$BASEDIR/drive$NEXT_DRIVE' (read-write)"
                    echo -e "\tYou may want your system to mount it every time you boot."
                    echo -e "\tTo do so, add this line VERY CAREFULLY to /etc/fstab and reboot:"
                    if [ "$uuid" == "" ]; then
                        echo -e "\t/dev/$drive $BASEDIR/drive$NEXT_DRIVE vfat rw,$FATOPTIONS 1 2"
                    else
                        echo -e "\tUUID=$uuid $BASEDIR/drive$NEXT_DRIVE vfat rw,$FATOPTIONS 1 2"
                    fi
                fi
            fi
        fi
    fi
done


for drive in $driveshfs; do
	if [[ $SELECTED_DRIVE != "" && $SELECTED_DRIVE != "/dev/$drive" ]]; then
		continue
	fi
    [ $VERBOSE == 'y' ] && echo "****************************************************************"
    uuid=`ls -l /dev/disk/by-uuid/ | grep "\.\./\.\./$drive" | awk '{print $(NF-2)}'`
    if [[ `grep "/dev/$drive" /etc/fstab` || `grep "UUID=$uuid" /etc/fstab` || `pmount | grep $drive` || `mount | grep $drive` ]]; then
        [[ $VERBOSE == 'y' && `pmount | grep $drive` ]] && echo "Ignoring /dev/$drive - already mounted"
        [[ $VERBOSE == 'y' && `mount | grep $drive` ]] && echo "Ignoring /dev/$drive - already mounted"
        [[ $VERBOSE == 'y' && `grep "/dev/$drive" /etc/fstab` ]] && echo "Ignoring /dev/$drive - already in /etc/fstab as /dev/$drive"
        [[ $VERBOSE == 'y' && `grep "UUID=$uuid" /etc/fstab` ]] && echo "Ignoring /dev/$drive - already in /etc/fstab as UUID=$uuid"
    else
        if [ -e /sys/block/${drive%?}/removable ]; then
            if [ "$(cat /sys/block/${drive%?}/removable)" == "1" ]; then
		        [ $VERBOSE == 'y' ] && echo "Ignoring /dev/$drive - removable drive"
            else
				while [ -e $BASEDIR/drive$NEXT_DRIVE ]; do
					NEXT_DRIVE=`echo $NEXT_DRIVE + 1 | bc`
				done
                mkdir $BASEDIR/drive$NEXT_DRIVE
                mount -t hfsplus -o rw,$HFSOPTIONS /dev/$drive $BASEDIR/drive$NEXT_DRIVE

                DRIVE_MOUNTED='y'
                if [ $VERBOSE == 'n' ]; then
					echo "$BASEDIR/drive$NEXT_DRIVE"
				else
                    echo "Mounted /dev/$drive as '$BASEDIR/drive$NEXT_DRIVE' (read-write)"
                    echo -e "\tYou may want your system to mount it every time you boot."
                    echo -e "\tTo do so, add this line VERY CAREFULLY to /etc/fstab and reboot:"
                    if [ "$uuid" == "" ]; then
                        echo -e "\t/dev/$drive $BASEDIR/drive$NEXT_DRIVE hfsplus $HFSOPTIONS 1 2"
                    else
                        echo -e "\tUUID=$uuid $BASEDIR/drive$NEXT_DRIVE hfsplus $HFSOPTIONS 1 2"
                    fi
                fi
            fi
        fi
    fi
done

for drive in $drivesext; do
	if [[ $SELECTED_DRIVE != "" && $SELECTED_DRIVE != "/dev/$drive" ]]; then
		continue
	fi
    [ $VERBOSE == 'y' ] && echo "****************************************************************"
    uuid=`ls -l /dev/disk/by-uuid/ | grep "\.\./\.\./$drive" | awk '{print $(NF-2)}'`
    if [[ `grep "/dev/$drive" /etc/fstab` || `grep "UUID=$uuid" /etc/fstab` || `pmount | grep $drive` || `mount | grep $drive` ]]; then
        [[ $VERBOSE == 'y' && `pmount | grep $drive` ]] && echo "Ignoring /dev/$drive - already mounted"
        [[ $VERBOSE == 'y' && `mount | grep $drive` ]] && echo "Ignoring /dev/$drive - already mounted"
        [[ $VERBOSE == 'y' && `grep "/dev/$drive" /etc/fstab` ]] && echo "Ignoring /dev/$drive - already in /etc/fstab as /dev/$drive"
        [[ $VERBOSE == 'y' && `grep "UUID=$uuid" /etc/fstab` ]] && echo "Ignoring /dev/$drive - already in /etc/fstab as UUID=$uuid"
    else
        if [ -e /sys/block/${drive%?}/removable ]; then
            if [ "$(cat /sys/block/${drive%?}/removable)" == "1" ]; then
		        [ $VERBOSE == 'y' ] && echo "Ignoring /dev/$drive - removable drive"
            else
				while [ -e $BASEDIR/drive$NEXT_DRIVE ]; do
					NEXT_DRIVE=`echo $NEXT_DRIVE + 1 | bc`
				done
                mkdir $BASEDIR/drive$NEXT_DRIVE
                mount -o rw,$EXTOPTIONS /dev/$drive $BASEDIR/drive$NEXT_DRIVE

                DRIVE_MOUNTED='y'
                if [ $VERBOSE == 'n' ]; then
					echo "$BASEDIR/drive$NEXT_DRIVE"
				else
                    echo "Mounted /dev/$drive as '$BASEDIR/drive$NEXT_DRIVE' (read-write)"
                    echo -e "\tYou may want your system to mount it every time you boot."
                    echo -e "\tTo do so, add this line VERY CAREFULLY to /etc/fstab and reboot:"
                    if [ "$uuid" == "" ]; then
                        echo -e "\t/dev/$drive $BASEDIR/drive$NEXT_DRIVE ext4 ${EXTOPTIONS:-defaults} 1 2"
                    else
                        echo -e "\tUUID=$uuid $BASEDIR/drive$NEXT_DRIVE ext4 ${EXTOPTIONS:-defaults} 1 2"
                    fi
                fi
            fi
        fi
    fi
done

if [ $VERBOSE == 'y' ]; then
    echo "****************************************************************"
    if [ $DRIVE_MOUNTED == 'y' ]; then
       echo "All Linux, Windows and Mac partitions on non-removable disks have been mounted"
    else
       echo "No usable Linux, Windows or Mac partitions found on non-removable disks"
    fi
fi
